#include "inputmanager.h"
#include "baseapplication.h"

using namespace EPOC;

void TextInput::Start(char cEndCondition)
{
}

void TextInput::Start()
{
}


void TextInput::End()
{
}


InputManager::InputManager(BaseApplication *pApplication)
{
	m_pApplication = pApplication;
	SetClassID(kCLASSID_INPUTMANAGER);

	m_pKeyboardInput = NULL;
	m_pMouseInput = NULL;
}

InputManager::~InputManager()
{
	InputManager::Cleanup();
}

void InputManager::Cleanup()
{
	if(m_pKeyboardInput)
		delete m_pKeyboardInput;
	m_pKeyboardInput = NULL;

	if(m_pMouseInput)
		delete m_pMouseInput;
	m_pMouseInput = NULL;
}

bool InputManager::Initialize()
{
	m_pKeyboardInput = new KeyboardInput();
	m_pMouseInput = new MouseInput();

	//SDL_EnableUNICODE(1);

	return true;
}

bool InputManager::Update(float fTimeDelta)
{	
	UNREFERENCED_PARAMETER(fTimeDelta);

	if(m_pMouseInput)
		m_pMouseInput->Reset();

	if(m_pKeyboardInput)
		ProcessKeyboard(NULL);

	return true;
}

void InputManager::HandleEvent(SDL_Event *pEvent)
{
	
	ProcessMouse(pEvent);
	//ProcessKeyboard(pEvent);

	
		//char ascii;
		//if(!(pEvent->key.keysym.unicode & 0xff80))
		//{
		//	ascii = (char)pEvent->key.keysym.unicode;
		//	if(ascii != 0)
		//	{

		//		m_sBufferedText += ascii;//int i = 10;

		//		if(ascii == 13)
		//			_asm int 3;
		//	}
		//}
  //  //else
  //       //<deal with international characters>;
}

void InputManager::ProcessKeyboard(SDL_Event *pEvent)
{
	UNREFERENCED_PARAMETER(pEvent);

	int nNumberKeys = 0;
	
	//Get the keystates 
	const Uint8 *pKeyStates = SDL_GetKeyboardState(&nNumberKeys);
	
	memcpy(&m_pKeyboardInput->m_rPrevKeyStates[0], &m_pKeyboardInput->m_rKeyStates[0], sizeof(Uint8) * SDLK_LAST);
	memcpy(&m_pKeyboardInput->m_rKeyStates[0], pKeyStates, sizeof(Uint8) * SDLK_LAST);
}

void InputManager::ProcessMouse(SDL_Event *pEvent)
{
	if(!m_pMouseInput) return;

	switch(pEvent->type ) 
	{
		case SDL_MOUSEMOTION:
		{
			m_pMouseInput->m_fX = pEvent->motion.x;
			m_pMouseInput->m_fY = pEvent->motion.y;
			m_pMouseInput->m_fRelX = pEvent->motion.xrel;
			m_pMouseInput->m_fRelY = pEvent->motion.yrel;
		}
		break;

		case SDL_MOUSEBUTTONUP:
		{
			if(pEvent->button.button == SDL_BUTTON_LEFT)
			{
				if(m_pMouseInput->m_bLeftDown)
					m_pMouseInput->m_bLeftPressed = true;

				m_pMouseInput->m_bLeftDown = false;
				m_pMouseInput->m_bLeftUp = true;
			}
			else if(pEvent->button.button == SDL_BUTTON_MIDDLE)
			{
				if(m_pMouseInput->m_bMiddleDown)
					m_pMouseInput->m_bMiddlePressed = true;

				m_pMouseInput->m_bMiddleDown = false;
				m_pMouseInput->m_bMiddleUp = true;
			}
			else if(pEvent->button.button == SDL_BUTTON_RIGHT)
			{
				if(m_pMouseInput->m_bRightDown)
					m_pMouseInput->m_bRightPressed = true;

				m_pMouseInput->m_bRightDown = false;
				m_pMouseInput->m_bRightUp = true;
			}
		}
		break;

		case SDL_MOUSEBUTTONDOWN:
		{
			if(pEvent->button.button == SDL_BUTTON_LEFT)
			{
				m_pMouseInput->m_bLeftUp = false;
				m_pMouseInput->m_bLeftDown = true;
				m_pMouseInput->m_bLeftPressed = false;
			}
			else if(pEvent->button.button == SDL_BUTTON_MIDDLE)
			{
				m_pMouseInput->m_bMiddleUp = false;
				m_pMouseInput->m_bMiddleDown = true;
				m_pMouseInput->m_bMiddlePressed = false;
			}
			else if(pEvent->button.button == SDL_BUTTON_RIGHT)
			{
				m_pMouseInput->m_bRightUp = false;
				m_pMouseInput->m_bRightDown = true;
				m_pMouseInput->m_bRightPressed = false;
			}
		}
		break;
	}	
}
